package ita.class1.group2.supermarket.merchant.controller;


import ita.class1.group2.supermarket.common.annotation.Autowired;
import ita.class1.group2.supermarket.common.annotation.Component;
import ita.class1.group2.supermarket.common.annotation.RequestMapping;
import ita.class1.group2.supermarket.common.auth.SecurityUtils;
import ita.class1.group2.supermarket.common.auth.Subject;
import ita.class1.group2.supermarket.common.auth.UsernamePasswordToken;
import ita.class1.group2.supermarket.common.auth.annotation.Permission;
import ita.class1.group2.supermarket.common.auth.exception.CredentialsException;
import ita.class1.group2.supermarket.common.auth.exception.UnknownAccountException;
import ita.class1.group2.supermarket.common.pojo.MessageResultCode;
import ita.class1.group2.supermarket.common.pojo.ResponseMessage;
import ita.class1.group2.supermarket.common.util.JsonUtil;
import ita.class1.group2.supermarket.common.util.Md5Utils;
import ita.class1.group2.supermarket.merchant.pojo.User;
import ita.class1.group2.supermarket.merchant.service.UserService;

import java.util.Map;

@Component
public class UserController {


    private static final String USER_NULL = "用户不存在";
    private static final String PWD_ERROR = "用户名密码错误";

    @Autowired
    SecurityUtils securityUtils;

    @Autowired
    UserService userService;



    @RequestMapping("/user/login")
    public String login(String jsonData) throws Exception {
        Subject subject = securityUtils.getSubject();
        Map<String,Object> reqMap = JsonUtil.json2map(jsonData);
        String username = (String)reqMap.get("username");
        String password = (String)reqMap.get("password");
        ResponseMessage responseMessage = new ResponseMessage();
        try {
            subject.login(new UsernamePasswordToken(username, Md5Utils.md5Password(password)));
            if (subject.isLogged()) {
                responseMessage.setResultCode(MessageResultCode.SUCCESS);
            }
        } catch (UnknownAccountException uk) {
            responseMessage.setResultCode(MessageResultCode.UNAUTHORIZED);
            responseMessage.setMessage(USER_NULL);
        } catch (CredentialsException ce) {
            responseMessage.setResultCode(MessageResultCode.UNAUTHORIZED);
            responseMessage.setMessage(PWD_ERROR);
        }
        return JsonUtil.obj2json(responseMessage);
    }




    @RequestMapping("/user/reg")
    public String regisiter(String jsonData) throws Exception {

        Map<String,Object> reqMap = JsonUtil.json2map(jsonData);
        User newUser = new User();
        newUser.setUsername((String)reqMap.get("username"));
        newUser.setPassword((String)reqMap.get("password"));
        ResponseMessage responseMessage = new ResponseMessage();
        if(userService.save(newUser)!=null){
            responseMessage.setResultCode(MessageResultCode.FIRSTLOGIN);
            Subject subject = securityUtils.getSubject();
            System.out.println(Md5Utils.md5Password(newUser.getPassword()));
            subject.login(new UsernamePasswordToken(newUser.getUsername(),newUser.getPassword()));
        }
        else {
            responseMessage.setResultCode(MessageResultCode.ERROR);
            responseMessage.setMessage("用户已存在");
        }
        return JsonUtil.obj2json(responseMessage);

    }


    @RequestMapping("/user/pwd")
    public String changePwd(String jsonData) throws Exception {
        Subject subject = securityUtils.getSubject();
        Map<String,Object> reqMap = JsonUtil.json2map(jsonData);
        String beforePassword = (String)reqMap.get("beforePassword");
        String newPassword = (String)reqMap.get("newPassword");
        ResponseMessage responseMessage = new ResponseMessage();

        User u  = (User)subject.getAuthorizingInfo();
        if(Md5Utils.md5Password(beforePassword).equals(u.getPassword())){
            u.setPassword(Md5Utils.md5Password(newPassword));
            responseMessage.setResultCode(MessageResultCode.SUCCESS);
        }
        else {
            responseMessage.setResultCode(MessageResultCode.ERROR);
            responseMessage.setMessage("原密码错误");
        }

        return JsonUtil.obj2json(responseMessage);
    }


}
